var index;

var table;
var form;

$(document).ready(function () {
    $('#page-toolbar-content').show();

    layui.use(['table', 'form'], function () {

        table = layui.table;
        form = layui.form;
        form.render();

        getAllButton(currUrl).done(function (returnData) {
            createButton(returnData);
            initButtonEvent();
            initTable();
        });

    });

});

function initButtonEvent() {

    var $ = layui.$, active = {

        add: function () {
            loadingChildPage('/admin/device/device_add', '新增设备');
        },
        edit: function () {
            var checkStatus = table.checkStatus('device-table-content');
            var data = checkStatus.data;
            if (data.length <= 0) {
                layer.msg("没有选取任何行！");
            }
            else if (data.length > 1) {
                layer.msg("无法批量修改行！");
            }
            else {
                loadingChildPageId('/admin/device/device_edit', "编辑设备", data[0].deviceId);
            }
        },
        del: function () {
            var checkStatus = table.checkStatus('device-table-content');
            var data = checkStatus.data;
            if (data.length <= 0) {
                layer.msg("没有选取任何行！");
            }
            else {
                var dataIds = [];
                for (var i = 0; i < data.length; i++) {
                    dataIds[i] = data[i].deviceId;
                }

                removeDevice(dataIds);
            }
        },
        check: function () {
            var checkStatus = table.checkStatus('device-table-content');
            var data = checkStatus.data;
            if (data.length <= 0) {
                layer.msg("没有选取任何行！");
            }
            else if (data.length > 1) {
                layer.msg("无法批量修改行！");
            }
            else {
                checkDevice(data[0].deviceId);
            }
        },
        search: function () {

            var deviceName = $('#deviceName');
            var deviceNo = $('#deviceNo');

            // 执行重载
            table.reload('device-table-content', {
                page: {
                    curr: 1
                }
                , where: {
                    deviceName: deviceName.val(),
                    deviceNo: deviceNo.val()
                }
            });

            $('.layui-table-page').css('text-align', 'right');
            $('.layui-table-page').css('padding-right', '20px');

        }
    };

    $('.menu-btn-group .layui-btn, .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

}

function initTable() {

    table.render({
        elem: '#device-table-content'
        , url: apiHostUrl + '/admin/device/queryDevice'
        , headers: {Authorization: token}
        , response: {
            statusName: 'errorCode'
            , statusCode: 0
            , msgName: 'errorMsg'
            , countName: 'totalCount'
            , dataName: 'returnData'
        }
        , cellMinWidth: 50
        , limit: 15
        , limits: [15]
        , cols: [[
            {type: 'checkbox'}
            , {field: 'deviceId', width: 130, title: 'ID'}
            , {field: 'deviceNo', width: 200, title: '设备编号'}
            , {field: 'deviceName', width: 200, title: '设备名称'}
            , {field: 'deviceManufacturer', title: '制造商'}
            , {field: 'devicePrice', width: 150, title: '设备购价'}
            , {field: 'deviceStartTime', width: 160, title: '启用时间'}
            , {field: 'createUserName', width: 150, title: '采购人员'}
            , {field: 'repairUserName', width: 150, title: '维修人员'}
            , {field: 'deviceStatusString', width: 70, title: '状态'}
            , {field: 'toolbar', width: 240, title: '操作', align: 'center', toolbar: '#tableToolbar'}
        ]]
        , page: {
            layout: ['prev', 'page', 'next', 'skip', 'limit', 'count']
        }
        , id: 'device-table-content'
    });

    // 监听工具条
    table.on('tool(page-table)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            var dataIds = [];
            dataIds[0] = data.deviceId;
            removeDevice(dataIds);
        } else if (obj.event === 'edit') {
            loadingChildPageId('/admin/device/device_edit', '编辑设备', data.deviceId);
        }
        else if (obj.event === 'check') {
            checkDevice(data.deviceId);
        }
    });

    $('.layui-table-page').css('text-align', 'right');
    $('.layui-table-page').css('padding-right', '20px');

}

function removeDevice(deviceIds) {
    layer.confirm("真的要删除该行吗？", function () {
        ajax(
            "/admin/device/delDevice", "POST",
            {deviceIds: deviceIds}
            , true, true, true
        ).done(function () {
            loadingPage("/admin/device/device");
        });
        layer.closeAll();
    });
}

function checkDevice(deviceId) {

    index = layer.open({
        title: "检修设备",
        type: 1,
        id: 'layer-page-page',
        shade: 0,
        area: ['450px', '252px'],
        content:
        '<div style="text-align: center;"></div><div style="padding: 20px;min-height: 100px;max-height: 100px;">' +
        '<form class="layui-form layui-form-pane" id="device-submit-form" onsubmit="return false;">' +
            '<div class="layui-form-item">' +
                '<label class="layui-form-label">*设备状态</label>' +
                '<div class="layui-input-block">' +
                    '<input type="radio" name="deviceCheckStatus" value="0" title="正常" checked>' +
                    '<input type="radio" name="deviceCheckStatus" value="1" title="异常">' +
                '</div>' +
            '</div>' +
            '<div class="layui-form-item">' +
                '<label class="layui-form-label">*检修说明</label>' +
                '<div class="layui-input-inline" style="min-width: 290px;max-width: 290px;">' +
                    '<input type="text" autocomplete="off" class="layui-input" name="deviceCheckDetail" id="deviceCheckDetail">' +
                '</div>' +
            '</div>' +
        '</form>' +
        '</div>' +
        '<hr>' +
        '<div class="layui-form-item" style="text-align: center;margin-bottom: 5px;">' +
        '<button class="layui-btn layui-btn-default"><i class="fa fa-check fa-fw""></i>确认</button>' +
        '<button class="layui-btn layui-btn-danger"><i class="fa fa-reply fa-fw"></i>返回</button></div></div>' +
        '<script>form.render();</script>',
        success: function (alertLayer) {
            var btn_ok = alertLayer.find('.layui-btn-default');
            btn_ok.attr({
                onclick: "javascript:addCheckDevice(" + deviceId + ");"
            });
            var btn_cancel = alertLayer.find('.layui-btn-danger');
            btn_cancel.attr({
                onclick: 'layer.closeAll();'
            });
        }
    });

}

function addCheckDevice(deviceId) {

    var deviceCheckStatus = $(":radio[name='deviceCheckStatus']:checked").val();
    var deviceCheckDetail = $('#deviceCheckDetail').val();

    ajax(
        "/admin/device/addCheckDevice", "POST",
        {
            deviceId: deviceId,
            deviceCheckStatus: deviceCheckStatus,
            deviceCheckDetail: deviceCheckDetail
        }
        , true, true, true
    ).done(function () {
        loadingPage("/admin/device/device");
    });
    layer.closeAll();
}
